package com.steerinfo.dil.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.IOException;

public class HttpUtil {
    public static JSONObject post(PostMethod postMethod, int connectTimeout, int socketTimeout) throws Exception {
        // 创建httpClient实例对象
        HttpClient httpClient = new HttpClient();
        // 设置httpClient连接主机服务器超时时间
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectTimeout);
        // 设置socket.timeout时间
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(socketTimeout);
        if(postMethod.getRequestHeader("Content-Type") == null){
            postMethod.addRequestHeader("Content-Type", "application/json;charset=utf-8");
        }
        httpClient.executeMethod(postMethod);
        String result = postMethod.getResponseBodyAsString();
        postMethod.releaseConnection();//释放连接
        return JSONObject.parseObject(result);
    }

    public static JSONObject delete(DeleteMethod deleteMethod, int connectTimeout, int socketTimeout) throws Exception {
        HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectTimeout);
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(socketTimeout);
        if(deleteMethod.getRequestHeader("Content-Type") == null){
            deleteMethod.addRequestHeader("Content-Type", "application/json;charset=utf-8");
        }
        httpClient.executeMethod(deleteMethod);
        String result = deleteMethod.getResponseBodyAsString();
        deleteMethod.releaseConnection();//释放连接
        return JSONObject.parseObject(result);
    }


    public static JSONObject sendPost(String url,JSONObject param) throws HttpException, IOException {
        // 创建httpClient实例对象
        HttpClient httpClient = new HttpClient();
        // 设置httpClient连接/执行超时时间(ms)
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(1000);
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(300000);
        // 创建post请求方法实例对象
        PostMethod postMethod = new PostMethod(url);
        postMethod.addRequestHeader("Content-Type", "application/json;charset=utf-8");
        // 构建参数
        RequestEntity entity = new StringRequestEntity(param.toJSONString(),"application/json", "UTF-8");
        postMethod.setRequestEntity(entity);
        //执行并处理返回
        httpClient.executeMethod(postMethod);
        String result = postMethod.getResponseBodyAsString();
        postMethod.releaseConnection();
        return JSONObject.parseObject(result);
    }

    public static JSONObject sendPostObject(String url,JSONObject param,String accessToken) throws HttpException, IOException {
        // 创建httpClient实例对象
        HttpClient httpClient = new HttpClient();
        // 设置httpClient连接/执行超时时间(ms)
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(1000);
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(15000);
        // 创建post请求方法实例对象
        PostMethod postMethod = new PostMethod(url);
        postMethod.addRequestHeader("Content-Type", "application/json;charset=utf-8");
        // 构建参数
        postMethod.setRequestHeader("Authorization",accessToken);
        RequestEntity entity = new StringRequestEntity(param.toJSONString(),"application/json", "UTF-8");
        postMethod.setRequestEntity(entity);
        //执行并处理返回
        httpClient.executeMethod(postMethod);
        String result = postMethod.getResponseBodyAsString();
        postMethod.releaseConnection();
        return JSONObject.parseObject(result);
    }

    public static JSONObject sendPostArray(String url, JSONArray array, String accessToken) throws HttpException, IOException {
        // 创建httpClient实例对象
        HttpClient httpClient = new HttpClient();
        // 设置httpClient连接/执行超时时间(ms)
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(1000);
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(15000);
        // 创建post请求方法实例对象
        PostMethod postMethod = new PostMethod(url);
        postMethod.addRequestHeader("Content-Type", "application/json;charset=utf-8");
        // 构建参数
        postMethod.setRequestHeader("Authorization",accessToken);
        //postMethod.setRequestHeader("Cookie",accessToken);
        RequestEntity entity = new StringRequestEntity(array.toJSONString(),"application/json", "UTF-8");
        postMethod.setRequestEntity(entity);
        //执行并处理返回
        httpClient.executeMethod(postMethod);
        String result = postMethod.getResponseBodyAsString();
        postMethod.releaseConnection();
        return JSONObject.parseObject(result);
    }

    public static String sendGet(String urlParam) throws HttpException, IOException {
        // 创建httpClient实例对象
        HttpClient httpClient = new HttpClient();
        // 设置httpClient连接主机服务器超时时间：15000毫秒
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
        // 创建GET请求方法实例对象
        GetMethod getMethod = new GetMethod(urlParam);
        // 设置post请求超时时间
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
        getMethod.addRequestHeader("Content-Type", "application/json;charset=utf-8");

        httpClient.executeMethod(getMethod);

        String result = getMethod.getResponseBodyAsString();
        getMethod.releaseConnection();
        return result;
    }
    public static void main(String[] args) throws HttpException, IOException {

    }
}
